package seismosurfer.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import seismosurfer.util.SeismoException;

/**
 * Don`t use this class anymore, instead use DB.
 * 
 * @deprecated Use DB instead.
 *
 */
public class DBUtil {

    protected static String ERROR_ON_CLOSING = "A database object could not be closed";

    private static DataSource dataSource = null;

    private static String dataSourceName = "jdbc/SeismoDB";

    private Connection dbConnection = null;

    private boolean connected = false;

    private boolean conFree = true;

    public DBUtil() {
        setDataSource(getDataSourceName());
        getConnected();
    }

    protected void getConnected() {

        // Load the Oracle JDBC driver
        try {
            dbConnection = getDataSource().getConnection();
            connected = true;
        } catch (SQLException e) {
            System.err.println("SQLException in DBUtil.getConnected: "
                    + e.getMessage());
            e.printStackTrace();
        }
    }

    public static String getDataSourceName() {
        return dataSourceName;
    }

    public static void setDataSourceName(String dsName) {
        dataSourceName = dsName;
    }

    public static DataSource getDataSource() {

        return dataSource;
    }

    public static void setDataSource(String dsName) {

        // Load the Oracle JDBC driver
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            setDataSourceName(dsName);
            dataSource = (DataSource) envCtx.lookup(getDataSourceName());
        } catch (NamingException e) {
            System.out.println("NamingException in DBUtil.getDataSource: "
                    + e.getMessage());
            e.printStackTrace();
        }

    }

    public synchronized Connection getDBConnection() {

        while (conFree == false) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        conFree = false;
        notify();

        return dbConnection;
    }

    public synchronized void releaseDBConnection() {

        while (conFree == true) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        conFree = true;
        notify();
    }

    public void closeDBConnection() {

        try {

            if (connected && dbConnection != null) {
                dbConnection.close();
                dbConnection = null;
                connected = false;
            }
        } catch (SQLException e) {
            System.out.println("SQLException in DBUtil.closeDBConnection () "
                    + e.getMessage());
            e.printStackTrace();
        } finally {

            if (dbConnection != null) {
                try {
                    dbConnection.close();
                    connected = false;
                } catch (SQLException e) {
                    System.out
                            .println("SQLException in DBUtil.closeDBConnection () "
                                    + e.getMessage());
                    e.printStackTrace();
                }
                dbConnection = null;
            }
        }

    }

    public static void close(ResultSet rs, Statement stm) {
        try {

            if (rs != null) {
                rs.close();
            }

            if (stm != null) {
                stm.close();
            }

        } catch (SQLException ex) {
            throw new SeismoException(DBUtil.ERROR_ON_CLOSING, ex);
        }

    }

    public void closeAll(ResultSet rs, Statement stm) {
        close(rs, stm);

        this.releaseDBConnection();
    }

}
